package com.mfangsoft.zhuangjialong.integration.statisticsactivity.contrller;





import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mfangsoft.zhuangjialong.app.entryactivity.model.BaseEntryActivity;
import com.mfangsoft.zhuangjialong.app.entryactivity.service.EntryActivityService;

import com.mfangsoft.zhuangjialong.common.model.ResponseMessage;
import com.mfangsoft.zhuangjialong.common.utils.SysConstant;
import com.mfangsoft.zhuangjialong.integration.order.model.OrderGift;
import com.mfangsoft.zhuangjialong.integration.statisticsactivity.model.BaseStatisticsActivity;
import com.mfangsoft.zhuangjialong.integration.statisticsactivity.service.StatisticsActivityService;




@Controller

@RequestMapping("/server")
public class StatisticsActivityController {
	
	@Autowired
	private StatisticsActivityService statisticsActivityService; //统计
	
	@Autowired
	private EntryActivityService entryActivityService; 			//报名
	
	/**
	 * 查询活动统计
	 * @param baseActivityInfo
	 * @return
	 */
	
	@RequestMapping(value="/selectStatisticsActivity/{activity_id}",method=RequestMethod.GET)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<BaseStatisticsActivity> selectStatisticsActivity(@PathVariable Integer activity_id){
		ResponseMessage<BaseStatisticsActivity> message = new ResponseMessage<BaseStatisticsActivity>();
		//BaseOrder baseOrder = new BaseOrder();

		try {
			
			BaseEntryActivity baseEntry = new BaseEntryActivity();
			
			//获取报名表中的数据
			baseEntry =entryActivityService.selectSum(activity_id);
			if(baseEntry.getConsume()!= null){
				BaseStatisticsActivity statistics =new BaseStatisticsActivity();
				statistics.setSign_state(baseEntry.getSign_state());
				statistics.setCard(baseEntry.getCard());
				//statistics.setEgg_ticket(baseEntry.getEgg_ticket());
				//statistics.setEgg_use_num(baseEntry.getEgg_use_num());
				//statistics.setLottery_ticket(baseEntry.getLottery_ticket());
				statistics.setAmount(baseEntry.getConsume());
				statistics.setActivity_id(activity_id);
				statistics.setOrder_num(baseEntry.getOrder_num());
				statisticsActivityService.updateStatistics(statistics);
			}
			//获取order_gift中的数据
			OrderGift orderGift = new OrderGift();
			orderGift =entryActivityService.selectOrderGift(activity_id);
			if(orderGift!=null){
				BaseStatisticsActivity statistics =new BaseStatisticsActivity();
			
				statistics.setEgg_ticket(orderGift.getEggcs());//金蛋次数
				statistics.setCoupon(orderGift.getJqcs());//抽奖次数
				statistics.setEgg_use_num(orderGift.getEgghs());//金蛋核销
				statistics.setCoupon_use(orderGift.getJqhs());//抽奖核销
				statistics.setCoupon_use(orderGift.getJqhs());//礼品数量
				statistics.setLottery_ticket(orderGift.getLottery_ticket());//礼品数量
				statistics.setAmount(orderGift.getReal_pay_price());//礼品数量
				
				statistics.setActivity_id(activity_id);
				statisticsActivityService.updateStatistics(statistics);
			}

			BaseStatisticsActivity sel =new BaseStatisticsActivity();
			sel.setActivity_id(activity_id);
		    message.setResult(statisticsActivityService.selectStatistics(sel));
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
		
		
	}

	
	
	
}
